package com.probox.common.entity.service.product.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;

import com.probox.common.core.constant.CachePrefix;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.BaseException;
import com.probox.common.core.exception.ProductException;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.redis.service.RedisService;
import com.probox.product.api.domain.ProductBrandVo;
import com.probox.common.entity.domain.product.ProductInfo;
import com.probox.common.entity.service.product.IProductInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.probox.common.entity.mapper.product.ProductBrandMapper;
import com.probox.common.entity.domain.product.ProductBrand;
import com.probox.common.entity.service.product.IProductBrandService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 商品品牌Service业务层处理
 *
 *
 * @date 2021-05-25
 */
@Service
@Transactional
public class ProductBrandServiceImpl extends ServiceImpl<ProductBrandMapper, ProductBrand> implements IProductBrandService {
    @Autowired
    private ProductBrandMapper productBrandMapper;

    @Autowired
    private RedisService redisService;

    /**
     * 查询商品品牌
     *
     * @param brandUid 商品品牌ID
     * @return 商品品牌
     */
    @Override
    public ProductBrand selectProductBrandById(Long brandUid) {
        return this.getById(brandUid);
    }

    @Override
    public ProductBrandVo getBrandVo(Long brandUid) {
        ProductBrandVo vo = new ProductBrandVo();
        ProductBrand byId = this.getById(brandUid);
        if(ObjectUtil.isEmpty(byId)) return null;
        BeanUtils.copyProperties(byId, vo);
        return vo;
    }


    /**
     * 查询商品品牌列表
     *
     * @param productBrand 商品品牌
     * @return 商品品牌
     */
    @Override
    public List<ProductBrand> queryList(ProductBrand productBrand) {
        QueryWrapper<ProductBrand> queryWrapper = Wrappers.query();
        if (StringUtils.isNotEmpty(String.valueOf(productBrand.getBrandName())) && productBrand.getBrandName() != null) {
            queryWrapper.like("BRAND_NAME", productBrand.getBrandName());
        }
        if (StringUtils.isNotEmpty(String.valueOf(productBrand.getDataStatus())) && productBrand.getDataStatus() != null) {
            queryWrapper.eq("DATA_STATUS", productBrand.getDataStatus());
        }
        Long platformId = SecurityUtils.getPlatformId();
        if(platformId == null){
            throw new BaseException("当前账号异常");
        }
        queryWrapper.eq("PLATFORM_UID", platformId);
        queryWrapper.ne("DATA_STATUS", DataStatus.delete.getKey());
        if (StringUtils.isNotEmpty(String.valueOf(productBrand.getDataPriority())) && productBrand.getDataPriority() != null) {
            queryWrapper.orderByAsc("DATA_PRIORITY");
        } else {
            queryWrapper.orderByAsc("DATA_PRIORITY");
            queryWrapper.orderByDesc("UPDATE_TIME");
        }

        return productBrandMapper.selectList(queryWrapper);
    }

    /**
     * 新增商品品牌
     *
     * @param productBrand 商品品牌
     * @return 结果
     */
    @Override
    public boolean insertProductBrand(ProductBrand productBrand) {
        Integer integer = queryName(productBrand.getBrandName(), null);
        if(integer> 0){
            throw new ProductException("名称不能重名！");
        }
        Long platformId = SecurityUtils.getPlatformId();
        productBrand.setPlatformUid(platformId);
        productBrand.setUpdateUid(SecurityUtils.getUserId());
        productBrand.setUpdateTime(DateUtil.date());
        productBrand.setUpdateName(SecurityUtils.getUsername());
        productBrand.setDataStatus(DataStatus.valid.getKey());
        redisService.deleteObject(CachePrefix.PRODUCT_BRAND_SELECT+platformId);
        return this.save(productBrand);
    }

    /**
     * 修改商品品牌
     *
     * @param productBrand 商品品牌
     * @return 结果
     */
    @Override
    public boolean updateProductBrand(ProductBrand productBrand) {
        Integer integer = queryName(productBrand.getBrandName(), productBrand.getBrandUid());
        if(integer> 0){
            throw new ProductException("名称不能重名！");
        }
        if(DataStatus.invalid.getKey().equals(productBrand.getDataStatus())){

        }
        productBrand.setUpdateUid(SecurityUtils.getUserId());
        productBrand.setUpdateName(SecurityUtils.getUsername());
        productBrand.setUpdateTime(DateUtil.date());
        redisService.deleteObject(CachePrefix.PRODUCT_BRAND_SELECT+SecurityUtils.getPlatformId());
        return this.updateById(productBrand);
    }

    /**
     * 批量删除商品品牌
     *
     * @param brandUids 需要删除的商品品牌ID
     * @return 结果
     */
    @Override
    public boolean deleteProductBrandByIds(Long[] brandUids) {
        // where in (?)
        UpdateWrapper<ProductBrand> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("BRAND_UID", Arrays.asList(brandUids));
        updateWrapper.set("DATA_STATUS", DataStatus.delete.getKey());
        updateWrapper.set("UPDATE_UID", SecurityUtils.getUserId());
        updateWrapper.set("UPDATE_NAME", SecurityUtils.getUsername());
        updateWrapper.set("UPDATE_TIME", DateUtil.date());
        return this.update(updateWrapper);
    }

    private Integer queryName(String name,Long uid){
        QueryWrapper<ProductBrand> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("BRAND_NAME", name);
        queryWrapper.eq("PLATFORM_UID", SecurityUtils.getPlatformId());
        queryWrapper.ne("DATA_STATUS", DataStatus.delete.getKey());
        if (uid != null) {
            queryWrapper.ne("BRAND_UID", uid);
        }
        return productBrandMapper.selectCount(queryWrapper);
    }


    /**
     *  获得品牌的下拉框
     * @return
     */
    @Override
    public List<ProductBrand> bulidSelect(){
        Long platformId = 1L;
        if(platformId == null){
            throw new BaseException("当前账号异常");
        }
        String key = CachePrefix.PRODUCT_BRAND_SELECT+platformId;
        List<ProductBrand> list = redisService.getCacheList(key);
        if(ObjectUtil.isEmpty(list)){
            QueryWrapper<ProductBrand> queryWrapper = Wrappers.query();
            queryWrapper.eq("PLATFORM_UID", platformId);
            queryWrapper.eq("DATA_STATUS", DataStatus.valid.getKey());
            queryWrapper.orderByAsc("DATA_PRIORITY");
            list = productBrandMapper.selectList(queryWrapper);
            if(ObjectUtil.isNotEmpty(list)) redisService.setCacheList(key, list);
        }
        return list;
    }
}
